package com.example.maybeaccount.Mode;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.example.maybeaccount.Entry.Entrance;

import com.example.maybeaccount.R;

import java.util.Calendar;
import java.util.TimeZone;

/**
 * 接受AlarmService发送的还款提醒请求，并发出提醒
 */
public class AlarmReceiver extends BroadcastReceiver {
    private NotificationManager manager;
    private static final int NOTIFICATION_ID_1 = 0x00113;
    private String title;
    private String content;

    @Override
    public void onReceive(Context context, Intent intent) {
    //此处接收闹钟时间发送过来的广播信息，为了方便设置提醒内容
        title = intent.getStringExtra("title");
        content = intent.getStringExtra("content");
        showNormal(context);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(context, AlarmService.class);
        context.startService(intent);  //回调Service,同一个Service只会启动一个，所以直接再次启动Service，会重置开启新的提醒，
    }
    /**
     * 发送通知
     **/

    private void showNormal(Context context) {
        Intent intent = new Intent(context, Entrance.class);//这里是点击Notification 跳转的界面，可以自己选择
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        //Notification notification = new Notification();
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("myChannelId","channelname"
                    ,NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道，通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }

        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Calendar calNow = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calNow.set(Calendar.HOUR_OF_DAY, calNow.get(Calendar.HOUR_OF_DAY));

        /** 还款时间段 */
        if(calNow.get(Calendar.HOUR_OF_DAY) >= 10 && calNow.get(Calendar.HOUR_OF_DAY) <= 18){
            Notification notification = new NotificationCompat.Builder(context,"myChannelId")
                    .setTicker(content)
                    .setContentInfo("还款提醒")
                    .setContentTitle(title)
                    .setContentText(content)
                    .setContentIntent(pi)
                    .setAutoCancel(true)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.logo)
                    //.setLargeIcon(R.mipmap.logo)
                    .build();
            manager.notify(1,notification);
        }
    }
}